package tool.soft

import java.io.File
import command.CommandExec
import dao._
import models.Tables
import org.joda.time.DateTime
import play.api.mvc.{AnyContent, Request}
import play.twirl.api.Html
import shared.Pojo._
import tool.Pojo._
import tool._
import tool.plot._
import tool.soft.AllSoft.SoftBT
import implicits.Implicits._
import models.Tables._
import play.api.data.FormBinding
import tool.softT.OplsdaSoftBT
import play.api.i18n.Messages
import utils.Utils
import upickle.default._

/**
 * Created by yz on 16/9/2020
 */
object OplsdaSoft extends OplsdaSoftBT with SoftBT {

  override def getResultFileName: String = {
    "Diff_Metabolite.csv"
  }

   override def produceConfigFile(workspaceDir: File,index:Int)(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    super.produceConfigFile(workspaceDir,index)
    Utils.t2ConfigFile(workspaceDir, data)
     Utils.t2ConfigFile(data, Tool.getCalculateConfigJsonFile(workspaceDir))
  }

  def getArg(implicit request: Request[AnyContent], messages: Messages, formBinding: FormBinding): ujson.Value = {
    val groupArg = if (data.group.isBlank) {
      ujson.Obj()
    } else ujson.Obj(messages("group") -> data.group)
    val scaleMethodMap=Tool.getScaleMethodMap
    ujson.Obj(messages("scaleMethod") ->scaleMethodMap(data.scaleMethod),messages("vipCutoff") -> data.vip, messages("q2yLowLimit") -> data.q2Y,
      messages("upLimitOfQ2Y") -> data.q2YB) ++ groupArg
  }

  override def getResultHtml(missionId: Int)(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.mission.oplsdaResult(missionId)
  }

  override def getHtml(missionName: String, missionId: Option[Int])(implicit request: Request[AnyContent], messages: Messages): Html = {
    views.html.user.soft.oplsda(missionName, missionId)
  }

  override def getMission(implicit request: Request[AnyContent], matrixFileDataDao: MatrixFileDataDao, messages: Messages, formBinding: FormBinding): Tables.MissionRow = {
    val initInputs = List(data.file, data.groupFile)
    val argJson = getArg
    MissionRow(id = 0, missionName = s"${data.missionName}", kind = nameStr, userId = userId,
      startTime = new DateTime(), state = "preparing", arg = argJson, input = initInputs)
  }

  override def produceInputFile(workspaceDir: File)(implicit request: Request[AnyContent], dao: MyDao, formBinding: FormBinding): Any = {
    val rawDataFile = new File(workspaceDir, "AllMet_Raw.txt")
    Tool.produceRawDataFile(userId, data.file, rawDataFile)

    val originalGroupFile = new File(workspaceDir, "original_group.txt")
    Tool.produceRawGroupFile(userId, data.groupFile, rawDataFile, originalGroupFile)
    val groupFile = Tool.getGroupFile(workspaceDir)
    if (data.group.isBlank) {
      groupFile.fileCopyToDir(workspaceDir)
    } else {
      val groupStr = data.group
      val groups = groupStr.mySplit(" vs ")
      originalGroupFile.txtLines.lineSeqMap.filter { map =>
        groups.contains(map("ClassNote"))
      }.csvLines.toTxtFile(groupFile)
    }
  }

  override def data(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    FormTool.oplsdaForm.bindFromRequest().get
  }


}

